#include "qmaskdialog.h"
#include "ui_qmaskdialog.h"

QMaskDialog::QMaskDialog(const QString &tips, QWidget *parent) : QDialog(parent), m_tips(tips), ui(new Ui::QMaskDialog)
{
    ui->setupUi(this);

    // setWindowFlags(Qt::Tool);		//当前窗口的构造函数下调用
    // setWindowFlags(Qt::ToolTip); 	//同时隐藏任务栏图标和标题栏图标

    // 子窗口在父窗口之上，但仍可以对父窗口进行操作
    // this->s/etWindowFlags(Qt::WindowStaysOnTopHint);		//当前窗口置于顶部
    // 如果要求只能操作子窗口，不能操作父窗口可以增加以下代码
    // this->setWindowModality(Qt::ApplicationModal); 		//阻塞除当前窗体之外的所有的窗体

    // 单击任务栏图标，对窗口进行显示/隐藏
    // this->setWindowFlags(Qt::WindowMinimizeButtonHint|Qt::WindowMaximizeButtonHint);
#if 1
    // 系统所有窗口的顶层包括桌面
    //  setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
    setAttribute(Qt::WA_TranslucentBackground, true);
    // showMaximized();
    showFullScreen();
#else
    // 当前应用程序的顶层窗口,exec调用，模态，无法关闭    show调用非模态，主窗口可接受close事件
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, true);
    showMaximized();
#endif
    // qDebug()<< QMovie::supportedFormats();
    // QMovie *movie=new QMovie("E:\\qt64test\\untitled2\\gif\\progress\\22222.gif");
    // movie->setScaledSize(QSize(50,50));
    // movie->start();
    // ui->label->setMovie(movie);
    // ui->label->move(1000,500);
    installEventFilter(this);
    // setAttribute(Qt::WA_DeleteOnClose);
    m_hook.hook(true);
    m_hook.enableTaskManager(true);
}

QMaskDialog::~QMaskDialog()
{
    delete ui;
    m_hook.hook(false);
    m_hook.enableTaskManager(false);
}

void QMaskDialog::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter painter(this);
    painter.fillRect(rect(), QColor(0, 0, 255, 20));
    QFont font = this->font();
    font.setBold(true);
    font.setPixelSize(80);
    painter.setFont(font);
    painter.drawText(rect(), Qt::AlignCenter, m_tips);
}

bool QMaskDialog::eventFilter(QObject *obj, QEvent *e)
{
    switch (e->type())
    {
    case QEvent::KeyPress:
    {
        QKeyEvent *ev = static_cast<QKeyEvent *>(e);
        if (ev->modifiers() == Qt::ControlModifier)
        {
            if (ev->key() == Qt::Key_U)
            {
                close();
                return true;
            }
        }
    }
    break;
    default:
        break;
    }
    return QDialog::eventFilter(obj, e);
}
